package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.enums.OperaPswEnum;
import com.tbit.uqbike.object.pojo.DepositApply;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.PageResult;
import com.tbit.uqbike.service.business.DepositApplyService;
import com.tbit.uqbike.service.business.OperaPswService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

/**
 * 押金退款申请
 *
 * @Description:
 * @Author: chen
 * @Date: 2024-01-09 13:39
 **/
@RestController
@RequestMapping("/depositApply")
public class DepositApplyController extends BaseController{

    @Autowired
    private DepositApplyService depositApplyService;
    @Autowired
    private OperaPswService operaPswService;

    /**
     * 分页查询押金退款申请记录
     *
     * @param phone 申请人手机号码
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param handelPhone 处理人手机号码
     * @param state 申请状态
     * @param page 页码数
     * @param size 每页大小
     * @return JsonResult
     */
    @RequestMapping("/list")
    public JsonResult list(Integer accountId,String phone, Long startTime, Long endTime, String handelPhone, Integer state
            , @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, HttpServletRequest request) {
        //去掉权限校验
//        LoginInfo loginUser = getLogin(request);
//        if(UserConstant.USER_ADMIN.equals(loginUser.type)){
//            Assert.notNull(accountId,"ver.accountId.notNull");
//        }else if(UserConstant.USER_BRAND.equals(loginUser.type)){
//            accountId=loginUser.id;
//        }else{
//            throw new BaseException("no_privilege");
//        }
        PageResult<DepositApply> result = depositApplyService.queryPage(accountId,phone, startTime, endTime, handelPhone, state, page, size);
        return JsonResult.ok(result.getList(), result.getTotal());
    }

    /**
     * 更新扣除押金
     *
     * @param applyId 申请id
     * @param state 处理状态 默认1成功
     * @param refundNO 退款单号
     * @param request 请求
     * @return JsonResult
     */
    @PostMapping("/editState")
    public JsonResult editState(@NotNull(message = "ver.applyId.notNull")Integer applyId,
                                @RequestParam(defaultValue = "1")Integer state, String refundNO, String secret,
                                HttpServletRequest request){
        LoginInfo loginUser = getLogin(request);

        /**操作密码校验*/
        operaPswService.checkOperaPsw(loginUser, OperaPswEnum.OPERA_PSW_MONEY_CONFIG.getModule(), secret);

        depositApplyService.editState(applyId,state,refundNO,loginUser.getAccountUserId(),loginUser.getPhone(),loginUser.name);
        return JsonResult.ok();
    }

}